﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Gx.Dal.Employee;
using Gx.Model.Entity;
using Gx.Model;
using Gx.Model.Enums;
using Gx.Common;
using Gx.Bll.Shared;

namespace Gx.Bll.Employee
{
    /// <summary>
    /// 级别逻辑层
    /// </summary>
    public class E_LevelInfoBll
    {
        private readonly E_DepLevelInfoBll dlBll = new E_DepLevelInfoBll();
        /// <summary>
        /// 级别
        /// </summary>
        /// <returns></returns>
        public List<E_LevelInfo> GetLevelInfoList()
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                return dal.GetLevelInfoList("");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        ///<param name="strWhere"></param>
        /// <returns></returns>
        private List<E_LevelInfo> GetLevelInfoList(string strWhere)
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                return dal.GetLevelInfoList(strWhere);
            }
        }

        /// <summary>
        /// 判断当前级别是否有效
        /// </summary>
        /// <param name="levelId"></param>
        /// <param name="levelName"></param>
        /// <returns></returns>
        public int IsExitsLevel(int levelId)
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                return dal.GetLevelInfoCount(string.Format("  AND  Id='{0}' ", levelId));
            }
        }

        /// <summary>
        /// 根据岗位Id获取实体
        /// </summary>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public E_LevelInfo GetLevelInfoModel(int levelId)
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                return dal.GetLevelInfoModel(levelId);
            }
        }

        public ReturnModel<bool> EditLevelInfo(E_LevelInfo model)
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.LevelName.IsNullOrEmpty())
                {
                    r.Msg = "岗位名称不能为空";
                    return r;
                }
                bool res = dal.EditLevelInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("EditLevelInfo", LoginHelper.GetUserId() + "编辑了岗位信息", model);
                    if (dlBll.EditE_DepLevelInfo(new E_DepLevelInfo() { DepId = model.DepId, LevelId = model.Id }))
                    {
                        r.Code = (int)Code.操作成功;
                        r.Msg = "操作成功";
                        r.Data = true;
                        r.Success = true;
                        return r;
                    }
                    r.Msg = "请联系系统管理员,Id:" + model.Id;
                    return r;
                }
                return r;
            }

        }

        public ReturnModel<bool> DelLevelInfo(int levelId)
        {

            using (LevelInfoDal dal = new LevelInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                bool res = dal.DelLevelInfo(levelId);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("EditLevelInfo", LoginHelper.GetUserId() + "删了了岗位信息", levelId);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Data = true;
                    r.Success = true;
                }
                return r;
            }
        }

        public ReturnModel<bool> AddLevelInfo(E_LevelInfo model)
        {
            using (LevelInfoDal dal = new LevelInfoDal())
            {
                ReturnModel<bool> r = new ReturnModel<bool>()
                {
                    Code = (int)Code.操作失败,
                    Msg = "操作失败",
                    Success = false,
                };
                if (model.LevelName.IsNullOrEmpty())
                {
                    r.Msg = "岗位名称不能为空";
                    return r;
                }
                int levelId = dal.AddLevelInfo(model);
                if (levelId > 0)
                {
                    new M_LogInfoBll().AddOptionsLog("AddLevelInfo", LoginHelper.GetUserId() + "新增了岗位信息", levelId);
                    if (dlBll.AddE_DepLevelInfo(new E_DepLevelInfo() { LevelId = levelId, DepId = model.DepId }))
                    {
                        r.Code = (int)Code.操作成功;
                        r.Msg = "操作成功";
                        r.Data = true;
                        r.Success = true;
                        return r;
                    }
                    r.Msg = "请联系系统管理员,Id:" + levelId;
                    return r;
                }
                return r;
            }
        }
    }
}
